package com.wgh;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;





@WebServlet("/Findstudent")
public class Findstudent extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public Findstudent() {
        super();
        
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			      Class.forName("com.mysql.jdbc.Driver");		
					String url = "jdbc:mysql://localhost:3306/jsp";	
					String username = "root";							
					String password = "root";							
					Connection conn = DriverManager.getConnection(url,username,password);
					Statement stmt = conn.createStatement();
					String sql = "select * from zhoujianyang";
					ResultSet rs = stmt.executeQuery(sql);
					List<student> list = new ArrayList<>();
					while(rs.next()) {
						student stu = new student();
						stu.setid(rs.getInt("id"));
						stu.setname(rs.getString("name"));
						stu.setinteresting(rs.getString("interesting"));
						list.add(stu);
					}
					request.setAttribute("list", list);
					rs.close();
					stmt.close();
					conn.close();
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		request.getRequestDispatcher("stuList.jsp").forward(request, response);
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
